﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.货号管理.service;
using DXApplicationTangche.DTO;
using DevExpress.XtraGrid.Demos.util;
using System.Net;
using DXApplicationTangche.UC.套号管理.套号下单;
using DXApplicationTangche.UC.货号管理.销售款式;
using DXApplicationTangche.UC.金蝶;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.抖音出入库.Service;

namespace DXApplicationTangche.UC.货号管理
{
    public partial class XtraUserControl样衣最新货号 : DevExpress.XtraEditors.XtraUserControl
    {
        private List<SaleStyleSample> 销售样衣s = new List<SaleStyleSample>();//销售样衣
        public XtraUserControl样衣最新货号()
        {
            InitializeComponent();
        }

        private void simpleButton刷新最新样衣_Click(object sender, EventArgs e)
        {
            this.销售样衣s = 货号管理Service.getSaleStyleSampleWithDateTime(0, 5000);
            this.gridControl销售样衣.DataSource = this.销售样衣s;
            this.advBandedGridView销售样衣.RefreshData();
        }

        private void 款式设计维护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String[] style = FunctionHelper.GetAdvBandedgridViewValue(
                    this.advBandedGridView销售样衣,
                    this.advBandedGridView销售样衣.FocusedRowHandle,
                    "styleNameCN").Split();
            new Form款式迭代一览(
                FunctionHelper.GetAdvBandedgridViewValue(
                    this.advBandedGridView销售样衣,
                    this.advBandedGridView销售样衣.FocusedRowHandle,
                    "saleStyleCode"),
                style[0],
                style[1]
                ).ShowDialog();
        }

        private void 套号二次下单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //成衣码
            String stockInventoryNo = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "stockInventoryNo");
            //货号面料码
            String saleInventoryMatialId = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "saleInventoryMatialId");
            if (new Model销售样衣().build最新样衣辅料(this.销售样衣s, saleInventoryMatialId).count <= 0)
            {
                DialogResult dialogResult = MessageBox.Show("该样衣无辅料清单，是否继续下单？", "是", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.No)
                {
                    return;
                }
            }
            this.splashScreenManager.ShowWaitForm();
            this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
            this.splashScreenManager.SetWaitFormDescription("正在初始化.....");     // 信息
            new Form套号销售样衣下单(stockInventoryNo, this.splashScreenManager).ShowDialog();
        }

        private void 查询样衣登记信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                //成衣码
                String stockInventoryNo = FunctionHelper.GetAdvBandedgridViewValue(
                                            this.advBandedGridView销售样衣,
                                            this.advBandedGridView销售样衣.FocusedRowHandle,
                                            "stockInventoryNo");
                //saleStyleId
                String saleStyleId = FunctionHelper.GetAdvBandedgridViewValue(
                                            this.advBandedGridView销售样衣,
                                            this.advBandedGridView销售样衣.FocusedRowHandle,
                                            "saleStyleId");
                DTO销售款式 dTO销售款式 = 货号管理Service.getSaleStyleBySaleStyleId(saleStyleId);
                foreach (SaleStyleSample saleStyleSample in this.销售样衣s)
                {
                    if (stockInventoryNo.Equals(saleStyleSample.stockInventoryNo))
                    {
                        new Form样衣登记(dTO销售款式, saleStyleSample).ShowDialog();
                        break;
                    }
                }
                //DTO销售款式 dTO销售款式 = 货号管理Service.getSaleStyleByBarcode(stockInventoryNo);
                //SaleStyleSample saleStyleSample = 货号管理Service.getSaleStyleSampleByBarcode(stockInventoryNo);
                //if (String.IsNullOrEmpty(saleStyleSample.stockInventoryNo))
                //{
                //    //  样衣登记进入了不确定款式，这里就差不到
                //    saleStyleSample.stockInventoryNo = stockInventoryNo;
                //}
                //new Form样衣登记(dTO销售款式, saleStyleSample).ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 样衣辅料维护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    //成衣码
            //    String stockInventoryNo = FunctionHelper.GetAdvBandedgridViewValue(
            //                            this.advBandedGridView销售样衣,
            //                            this.advBandedGridView销售样衣.FocusedRowHandle,
            //                            "stockInventoryNo");
            //    String saleInventoryMatialId = FunctionHelper.GetAdvBandedgridViewValue(
            //                                this.advBandedGridView销售样衣,
            //                                this.advBandedGridView销售样衣.FocusedRowHandle,
            //                                "saleInventoryMatialId");
            //    new Form销售样衣辅料维护(stockInventoryNo, saleInventoryMatialId).ShowDialog();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
        }

        private void 物料清单维护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String styleNameCN = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "styleNameCN");
                String materialCode = FunctionHelper.GetAdvBandedgridViewValue(
                                            this.advBandedGridView销售样衣,
                                            this.advBandedGridView销售样衣.FocusedRowHandle,
                                            "materialCode");
                if (String.IsNullOrEmpty(materialCode))
                {
                    return;
                }
                //new Form金蝶EBOM保存(styleNameCN, materialCode).ShowDialog();
                new Form金蝶EBOM修改(styleNameCN, materialCode).ShowDialog();
                //new Form金蝶EBOM修改("20FJ40 女长款V领微收腰小香风马甲", "JQ-20721").ShowDialog();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void advBandedGridView销售样衣_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try
            {
                if (e.Column.Name == "bandedGridColumn是否维护物料清单")
                {
                    if (Convert.ToBoolean(e.CellValue.ToString()))
                    {
                        e.Appearance.BackColor = Color.LightGreen;
                    }
                    else
                    {
                        e.Appearance.BackColor = Color.LightPink;
                    }
                }
            }
            catch { }
        }

        private void 登记到抖音款式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String refBarcode = FunctionHelper.GetAdvBandedgridViewValue(
                                        this.advBandedGridView销售样衣,
                                        this.advBandedGridView销售样衣.FocusedRowHandle,
                                        "stockInventoryNo");
            OrderCommitDTO返回 orderCommitDTO =
                                订单预览新Service.getOrderCommitDTOByBarcode(refBarcode);
            try { orderCommitDTO.styleSizeCd = orderCommitDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
            //若尺码为空，默认M
            if (String.IsNullOrEmpty(orderCommitDTO.styleSizeCd))
            {
                orderCommitDTO.styleSizeCd = "M";
            }
            //若款式为样衣，无refBarcode，取成衣码barcode
            if (String.IsNullOrEmpty(orderCommitDTO.refBarcode))
            {
                orderCommitDTO.refBarcode = refBarcode;
            }
            //判断抖音款式是否存在
            List<DouyinMaterialDTO> douyinMaterialDTOs = 抖音Service.getDouyinMaterialAll();
            DouyinMaterialDTO douyinMaterialDTO = new DouyinMaterialDTO(orderCommitDTO.refBarcode, orderCommitDTO.styleNameCn, orderCommitDTO.materialCode, orderCommitDTO.styleSizeCd);
            //foreach(DouyinMaterialDTO douyinMaterialDTO1 in douyinMaterialDTOs)
            //{
            //    if(douyinMaterialDTO1)
            //}
            抖音Service.saveDouyinMaterial(douyinMaterialDTO);
        }
    }
}
